package dept

import (
	"context"
	"fast-boot/app/api/admin/internal/svc"
	"fast-boot/app/api/admin/internal/types"
	"fast-boot/app/model"

	"github.com/zeromicro/go-zero/core/logx"
)

type OptionsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 部门下拉列表
func NewOptionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OptionsLogic {
	return &OptionsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *OptionsLogic) Options() (resp []*types.SysDeptOption, err error) {
	deptModel := model.SysDeptModel{}
	var items []*model.SysDeptModel
	l.svcCtx.GormClient.GormDb.Model(deptModel).
		Where("status = 1").
		Order("sort asc, id asc ").
		Scan(&items)

	return SysDeptOptionGetTree(items, 0), nil
}

// GetTree 转化树结构
func SysDeptOptionGetTree(list []*model.SysDeptModel, parentID int64) []*types.SysDeptOption {
	res := make([]*types.SysDeptOption, 0)
	for _, item := range list {
		if item.ParentID == parentID {
			var dept types.SysDeptOption
			dept.Label = item.Name
			dept.Value = item.Id
			dept.Children = SysDeptOptionGetTree(list, item.Id)
			res = append(res, &dept)
		}
	}
	return res
}
